#include "qtimeedithost.h"


#include "qcommonstruct.h"
#include "qabstractproperty.h"
#include "qpropertyfactory.h"

#include <QDateTimeEdit>

using namespace SHAREDLIB;

QTimeEditHost::QTimeEditHost(QObject *object):
    QAbstractSpinBoxHost(object)
{
    QAbstractProperty *pro;

    pro=QPropertyFactory::createProperty("Time");
    if(pro!=NULL)
    {
        pro->setAttribute("name","maximumTime");
        pro->setAttribute("property_name",tr("MaximumTime"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Time");
    if(pro!=NULL)
    {
        pro->setAttribute("name","minimumTime");
        pro->setAttribute("property_name",tr("MinimumTime"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Time");
    if(pro!=NULL)
    {
        pro->setAttribute("name","time");
        pro->setAttribute("property_name",tr("Time"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Enum");
    if(pro!=NULL)
    {
        pro->setAttribute("name","currentSection");
        pro->setAttribute("property_name",tr("CurrentSection"));
        pro->setAttribute("group","Attributes");
        ComboItems items;
        tagComboItem item;

        item.m_text=tr("HourSection");
        item.m_value=QDateTimeEdit::HourSection;
        items.append(item);

        item.m_text=tr("MinuteSection");
        item.m_value=QDateTimeEdit::MinuteSection;
        items.append(item);

        item.m_text=tr("SecondSection");
        item.m_value=QDateTimeEdit::SecondSection;
        items.append(item);

        QVariant v;
        v.setValue<ComboItems>(items);
        pro->setAttribute("items",v);
        pro->setValue(QDateTimeEdit::HourSection);
        addProperty(pro);
    }


    pro=QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","timeChanged");
        pro->setAttribute("property_name",tr("TimeChanged"));
        pro->setAttribute("group","Events");
        m_object->setProperty("timeChanged","");
        addProperty(pro);
    }

    QDateTimeEdit* e=(QDateTimeEdit*)m_object;
    connect(e,SIGNAL(timeChanged(QTime)),this,SLOT(timeChanged(QTime)));
}

void QTimeEditHost::setTime(int hour, int minute, int second)
{
    QTime ti(hour,minute,second);
    setPropertyValue("time",ti);
}

int QTimeEditHost::hour()
{
    QTime ti=getPropertyValue("time").toTime();
    return ti.hour();
}

int QTimeEditHost::minute()
{
    QTime ti=getPropertyValue("time").toTime();
    return ti.minute();
}

int QTimeEditHost::second()
{
    QTime ti=getPropertyValue("time").toTime();
    return ti.second();
}

void QTimeEditHost::setCurrentSection(int currentSection)
{
    setPropertyValue("currentSection",currentSection);
}

int QTimeEditHost::currentSection()
{
    return getPropertyValue("currentSection").toInt();
}

void QTimeEditHost::timeChanged(const QTime &time)
{
    QString code=getPropertyValue("stateChanged").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("hour",QString::number(time.hour()));
        param.insert("minute",QString::number(time.minute()));
        param.insert("second",QString::number(time.second()));
        emit_exec(code,param);
    }
}
